/**
 * Created by ali on 2017/12/4.
 */
var flag; //设置添加或修改的标识
//var basePath = "http://127.0.0.1/GraduationProject";
$(function() {
    loadData();
});

//加载数据
function loadData() {
    $.ajax({
		type:"get",
		url:basePath +"/productType/listAll",
		async:true,
		success:function(data){
			if(data){
				$('#dataTable').bootstrapTable("loadByRecords",data.obj)
				.on('page-change.bs.table', function (e, number, size) {
					updateRecordsByPage (number,size);
				})
			}
		}
	});
}

/**
 * 点击下一页或者是上一页的时候进行分页
 * 
 * @param number
 *            页码
 * @param size
 *            数量
 * @returns
 */
function updateRecordsByPage(number,size){
	var page = {};
	page.size=size;
	page.current=number;
	$.ajax({
		type:"get",
		url:basePath +"/productType/listAll",
		data : {"page":JSON.stringify(page)},
		async:false,
		success:function(data){
			if(data){
				$('#dataTable').bootstrapTable("loadByRecords",data.obj)
			}
		}
	});
}

/**
 * 模糊查询
 * @returns
 */
function doQuery(){
	var key = $("#keyWord").val();
	var page = {}
	page.condition={}
	page.condition.classname=key;
	$.ajax({
		type : "get",
		url :basePath + "/productType/listByCondition",
		data : {"page":JSON.stringify(page),"isSearch":1},
		success : function(data) {
			if (data) {
				dataobj = data.obj.records;
				$('#dataTable').bootstrapTable("loadByRecords",data.obj)
			}
		},
		error:function (data){
			console.log(data);
		}
	});
}

/**
 * form序列化成对象
 */
$.fn.serializeObject = function()      
{      
   var o = {};      
   var a = this.serializeArray();      
   $.each(a, function() {      
       if (o[this.name]) {      
           if (!o[this.name].push) {      
               o[this.name] = [o[this.name]];      
           }      
           o[this.name].push(this.value || '');      
       } else {      
           o[this.name] = this.value || '';      
       }      
   });      
   return o;      
};   

//添加
function add() {
    var a = $('#addForm').serializeObject();
	$("#addForm")[0].reset();
	$("#addModal").modal('hide');
	$.ajax({
	type:"post",
	url:basePath +"/productType/add",
	data:a,
	ContentType:"application/x-www-form-urlencoded;charset=utf-8",
	async:true,
	success:function(data){
		if(data){
			alert(data.msg);
			document.location.reload();
		}
	},error:function(data){
		alert(data.msg);
		document.location.reload();
	}
	});
   
}

//修改
function modify() {
	var json = $("#modifyForm").serializeObject(); 
	$("#modifyForm")[0].reset();
	$("#modifyModal").modal('hide');
    $.ajax({
		type:"post",
		url:basePath +"/productType/update",
		data:json,
		ContentType:"application/x-www-form-urlencoded;charset=utf-8",
		async:true,
		success:function(data){
				alert(data.msg);
				document.location.reload();
		},
		error: function(data){
			alert(data.msg);
			document.location.reload();
		}
	});
}

//保存
function save() {
    
}

//取消
function cancel() {
}

//删除
function remove() {
	var id = $("#cid").val(); 
    $.ajax({
		type:"post",
		url:basePath +"/productType/del/" +id, 
		async:true,
		success:function(data){
				alert(data.msg);
				document.location.reload();
		},
		error: function(data){
			alert(data.msg);
			document.location.reload();
		}
	});
}

/**
 * 格式化操作栏
 * @param {Object} value
 * @param {Object} row
 * @param {Object} index
 */
function operateFormatter(value,row,index){
    return '<a href="javascript:;"'
    + 'data-classname="'+row.classname+'"'
    + 'data-isParent="'+row.isparent+'"'
    + 'data-parentId="'+row.parentId+'"'
    + 'data-id="'+row.id+'"'
    + 'data-target="#modifyModal" data-toggle="modal" class="modifyBtn"  style="color:#31B0D5;">修改</a> | <a href=""  data-toggle="modal" data-cid="'+row.id+'" data-target="#delModel" id="removeBtn" style="color:#31B0D5;">删除</a>'
}


function formatImg(value,row,index){
	var imgs = value.split(",");
	var divHtml = "";
	for(var i in imgs){
		divHtml += "<img src='"+imgs[i]+"'>";
	}
  	return divHtml;
}

/**
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function parentFormatter (value, row, index) {
	return  value==1 ? "是" : "否";
}


/**
 * 格式化时间
 * 
 * @param {Object}
 *            value
 * @param {Object}
 *            row
 * @param {Object}
 *            index
 */
function dateFormatter(value, row, index) {
	return  (new Date(value)).Format("yyyy-MM-dd hh:mm:ss");
}

/**
 * 格式化时间
 */
Date.prototype.Format = function (fmt) { //author: tony  
    var o = {  
        "M+": this.getMonth() + 1, //月份  
        "d+": this.getDate(), //日  
        "h+": this.getHours(), //小时  
        "m+": this.getMinutes(), //分  
        "s+": this.getSeconds(), //秒  
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
        "S": this.getMilliseconds() //毫秒  
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
};
